package com.example.xloong.mymvvm.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Xloong on 2018/1/26.
 */

public abstract class BaseAdapter<T,VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {

    public Context mContext;
    public List<T> mList;
    public LayoutInflater inflater;

    public BaseAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        inflater = (LayoutInflater) mContext.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getItemCount() {
        return mList.size()== 0 ?0:mList.size();
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
       return onCreateVH(parent,viewType);
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {

    }


    /**
     *
     * 创建View Holder
     *
     * @param parent
     * @param viewType
     * @return
     */


    public abstract VH onCreateVH(ViewGroup parent,int viewType);


    /**
     *
     * 绑定 View Holder
     *
     * @param holder
     * @param positoin
     */

    public abstract void onBindView(VH holder,int positoin);

    /**
     * 刷新数据
     *
     * @param data  数据源
     */

    public void refreshData(List<T> data){
        mList.clear();
        mList.addAll(data);
        notifyDataSetChanged();
    }


    /**
     *
     * 加载更多
     *
     * @param data  数据源
     */


    public void loadMoreData(List<T> data){

        mList.addAll(data);
        notifyDataSetChanged();
    }



}
